<?php

$libelle = NULL;
$idEdit = NULL;

if(isset($_POST['idCat']) && isset($_POST['delete'])) //Deleting a category
{
	Category::delete($_POST['idCat']);
}

if(isset($_POST['idCat']) && isset($_POST['edit'])) //Loading category's datas
{
	$cat = Category::read($_POST['idCat']);
	$libelle = $cat->getNameCat();
	$idCatParent = $cat->getIdParentCat();
	$idEdit = $_POST['idCat'];	
}

if(isset($_POST['idEdit']) && isset($_POST['libelle']) && isset($_POST['catParent'])) //Editing a category
{
	
	$editCat = Category::read($_POST['idEdit']);
	$editCat->setNameCat($_POST['libelle']);
	$editCat->setIdParentCat($_POST['catParent']);
	$editCat->save();
}

if(isset($_POST['libelle']) && empty($_POST['idEdit'])) //Creating a new category
{
	$newCat = new Category($_POST['libelle'], $_POST['catParent']);
	$newCat->save();
}

$mainCategories = Category::listing("WHERE CAT_IdParent=0 ORDER BY CAT_Libelle");

include_once('partials/list_categories.php');
		

?>